<template>
  <div class="registerDetail">
    <van-nav-bar :fixed="true" :placeholder="true" :title="$route.meta.title" left-text="返回" left-arrow
      @click-left="onClickLeft" />
    <!-- 单车详情 -->

    <!-- 表单 -->
    <div class="formData">
      <van-field readonly v-model="dataSource.fixCenterName" name="所属维修中心" label="所属维修中心" input-align="right" />

      <van-field v-model="dataSource.serverPsnName" name="所属服务顾问" label="所属服务顾问" input-align="right" readonly />
      <van-field v-model="dataSource.sellPsnName" name="所属销售人员" label="所属销售人员" input-align="right" readonly />

      <van-field v-model="dataSource.vinCode" name="车架号" label="车架号" input-align="right" readonly />
      <van-field v-model="dataSource.carNo" name="车牌号" label="车牌号" input-align="right" readonly />

      <van-field v-model="dataSource.carModelNameName" name="车型" label="车型" input-align="right" readonly />
      <van-field v-model="dataSource.style" name="款式（年款）" label="款式（年款）" input-align="right" readonly />

      <van-field v-model="dataSource.lastIn" name="上次进场时间" label="上次进场时间" input-align="right" readonly />
      <van-field v-model="dataSource.carMile" name="里程数" label="里程数" input-align="right" readonly>
        <template slot="button">km</template>
      </van-field>
      <!-- <van-field
        v-model="dataSource.mileage"
        name="标签"
        label="标签"
        input-align="right"
        readonly
        class="label_box"
      >
        <template slot="input">
          <div
            class="label_item"
            v-for="(item, index) in dataSource.label"
            :key="index"
          >
            {{ item }}
          </div>
        </template>
      </van-field> -->
    </div>
    <!-- 底部tab -->
    <div class="tabBarBox">
      <van-tabs v-model="tabActive">
        <van-tab title="车辆">
          <div class="tabCar">
            <van-field v-model="dataSource.totalPrp" name="车辆属性值" label="车辆属性值" input-align="right" readonly></van-field>
            <van-field v-model="dataSource.deliverTime" name="购车时间" label="购车时间" input-align="right" readonly></van-field>
            <van-field v-model="dataSource.inusuranceDate" name="保险到期日" label="保险到期日" input-align="right"
              readonly></van-field>
            <van-field v-model="dataSource.inusuranceNumber" name="最近保险登记单" label="最近保险登记单" input-align="right" readonly
              @click="handleInsurance" label-width="130px"></van-field>
            <van-field v-model="dataSource.inusuranceType" name="保险类型" label="保险类型" input-align="right"
              readonly></van-field>
            <van-field v-model="dataSource.inusuranceComName" name="保险公司" label="保险公司" input-align="right"
              readonly></van-field>
            <van-field v-model="dataSource.repairtime" name="保修到期日" label="保修到期日" input-align="right"
              readonly></van-field>
            <van-field v-model="dataSource.carType" name="车辆来源" label="车辆来源" input-align="right" readonly></van-field>
            <!-- <van-field
              v-model="dataSource.rmember"
              name="是否会员客户"
              label="是否会员客户"
              input-align="right"
              readonly
            ></van-field> -->
            <van-field v-model="dataSource.memberTies" name="是否会员绑定车辆" label="是否会员绑定车辆" input-align="right"
              label-width="170px" readonly></van-field>
            <van-field v-model="dataSource.resSell" name="车辆是否已卖" label="车辆是否已卖" input-align="right" readonly></van-field>
            <van-field v-model="dataSource.timer" name="车辆附件" label="车辆附件" input-align="right" right-icon="arrow"
              placeholder="点击进行车辆附件管理" readonly @click="goFile"></van-field>
          </div>
        </van-tab>
        <van-tab title="卡券">
          <div class="tabCar">
            <div class="cardList" v-if="cardList.length != 0">
              <div class="cardItem" v-for="(item, index) in cardList" :key="index">
                <div class="box flex">
                  <div class="cardItem_lt flex">
                    <div class="price_icon">¥</div>
                    <div class="price">
                      <div class="txt">{{ item.amount }}</div>
                      <div class="name">{{ item.couponsName }}</div>
                    </div>
                  </div>
                  <div class="cardItem_rt">
                    <div class="label_name flex">
                      <div class="label_txt">{{ item.typeName }}</div>
                      <div class="label_txt1">{{ item.name }}</div>
                    </div>
                    <div class="label_time">{{ item.usedDate }}领用</div>
                    <div class="label_time">{{ item.expireDate }}到期</div>
                  </div>
                </div>
                <div class="cardNo">券号{{ item.ticketNumber }}</div>
              </div>
            </div>
            <van-empty class="noContent" description="暂无数据" v-else />
          </div>
        </van-tab>
        <van-tab title="维修履历">
          <div class="tabCar">
            <div class="repairData" v-if="repairList.length != 0">
              <div class="repairItem" v-for="(item, index) in repairList" :key="index" @click="goRepairDetail(item)">
                <div class="item_title flex">
                  <div class="txt">履历编号：{{ item.repairNumber }}</div>
                  <i class="iconfont icon-right"></i>
                </div>
                <div class="item_content">
                  <div class="item_contentbox flex">
                    <div class="item_data flex">
                      <div class="item_data_label">工单类型：</div>
                      <div class="item_data_value">
                        {{ item.repairWorkBillType }}
                      </div>
                    </div>
                    <div class="item_data flex">
                      <div class="item_data_label">服务顾问：</div>
                      <div class="item_data_value">
                        {{ item.serverPersonName }}
                      </div>
                    </div>
                  </div>

                  <div class="item_content_data">
                    <div class="item_data flex">
                      <div class="item_data_label">进场时间：</div>
                      <div class="item_data_value">
                        {{ item.repairBizDate }}
                      </div>
                    </div>
                    <div class="item_data flex">
                      <div class="item_data_label">合计金额：</div>
                      <div class="item_data_value">
                        {{ item.repairVoucherAmt }}元
                      </div>
                    </div>
                    <div class="item_data flex">
                      <div class="item_data_label">卡券抵用金额：</div>
                      <div class="item_data_value">
                        {{ item.repairTotalSum }}元
                      </div>
                    </div>
                    <div class="item_data item_remark">
                      <div class="item_data_label item_data_width">
                        人工操作：
                      </div>
                      <div class="item_data_value txt">
                        {{ item.repairRstRemark }}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <van-empty class="noContent" description="暂无数据" v-else />
          </div>
        </van-tab>
        <van-tab title="联系人">
          <div class="tabCar">
            <div class="contactbox">
              <div class="contactItem flex">
                <div class="contact_lt">
                  <div class="header flex">
                    <div class="txt">客户</div>
                    <div class="name">{{ dataSource.customerName }}</div>
                  </div>
                  <div class="tel">{{ dataSource.mobile }}</div>
                </div>
                <div class="contact_rt">
                  <a @click="handleTel(dataSource.mobile)">
                    <i class="iconfont icon-tel"></i>
                  </a>
                  <i class="iconfont icon-wx" @click.stop="getExList(dataSource)"></i>
                </div>
              </div>
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>

    <div class="addBtn">
      <div class="orderItem_filebox" @click="applyRepair">
        <i class="iconfont icon-add"></i>
        维修预约
      </div>
    </div>
    <van-dialog v-model="showDialog" title="" show-cancel-button :showConfirmButton="false">
      <div class="contact_list">
        <div class="contact_item flex" v-for="(item, index) in contactList" :key="index">
          <div class="contact_lt flex">
            <img v-if="item.avatar" class="contact_header" :src="item.avatar" />
            <div class="contact_name">
              {{ item.remark ? item.remark : item.name }}
            </div>
            <div class="contact_com_wx" v-if="item.type == 1">@微信</div>
            <div class="contact_com" v-if="item.type == 2">
              @{{ item.corpName ? item.corpName : item.corpFullName }}
            </div>
          </div>
          <div class="contact_btn" @click="runWxChat(item)">联系</div>
        </div>
      </div>
    </van-dialog>
  </div>
</template>

<script>
import {
  Search,
  Empty,
  Field,
  Form,
  Uploader,
  DatetimePicker,
  Tab,
  Tabs,
  Toast,
  Notify,
  Dialog,
} from 'vant'
import carApi from '@/api/carApi'
import repairApi from '@/api/repairApi'
import { getCookies, isPhone } from '@/utils/util.js'
import axios from 'axios'
import qs from 'qs'
const base_url = process.env.BASE_API
export default {
  name: 'registerDetail',
  components: {
    Search,
    Empty,
    Field,
    Form,
    Uploader,
    DatetimePicker,
    Tab,
    Tabs,
    Notify,
    Dialog,
    Toast,
  },
  data() {
    return {
      showTimerPicker: false,
      dataSource: {
        id: '', //单车id
        vinCode: '', //车架号
        fixCenterId: '', //维修中心ID
        fixCenterName: '', //维修中心名称
        fixCenterNumber: '', //维修中心编码
        serverPsnId: '', //所属服务顾问ID
        serverPsnName: '', //所属服务顾问名字
        serverPsnNumber: '', //所属服务顾问编码
        carModelId: '', //车型ID
        carModelNameName: '', //车型名称
        carModelNumber: '', //车型编码
        lastIn: '', //上次进场时间
        carMile: '', //行驶里程数
        deliverTime: '', //购车时间
        inusuranceDate: '', //保险到期时间
        repairtime: '', //保修到期时间
        label: [], //标签
        style: '', //款式，年款
        sellPsnId: '', //所属销售人员
        sellPsnName: '', //所属销售人员
        carNo: '',
        totalPrp: '',
        memberTies: '', //是否会员绑车
        resSell: '', //车辆是否已转卖
        rmember: '', //是否会员客户
        inusuranceNumber: '', //最近保险单编号
        inusuranceId: '', //保险单ID
        inusuranceComName: '', //保险公司
        inusuranceType: '', //保险类型
      },
      uploader: [],
      columns: [],
      showSelectPicker: false,
      disabled: false,
      billId: '',
      tabActive: 0,
      cardList: [], //卡券列表
      repairList: [],
      showDialog: false,
      contactList: [],
    }
  },

  mounted() {
    if (this.$route.query) {
      this.billId = this.$route.query.billId
      this.getCarInfoBase(this.billId)
      this.getCoupon(this.billId)
      this.getRepairworkorder(this.billId)
    }
  },
  methods: {
    // 获取外部联系人信息
    getExList(data) {
      axios({
        url: `${base_url}/proxy/ext/user/getExList`,
        method: 'post',
        data: qs.stringify(
          // { url: 'http://wycl.autocnd.com:41080/ysc/assessManageDetail' },
          { carNo: data.carNo, name: data.customerName },
          { encode: false },
        ),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          satoken: getCookies('token'),
        },
      }).then((res) => {
        if (res.data && res.data.data && res.data.data.length != 0) {
          this.contactList = res.data.data
          this.showDialog = true
        } else {
          Notify({ type: 'danger', message: '未找到相关联系人' })
        }

        console.log('获取外部联系人信息:', res)
      })
    },
    runWxChat(data) {
      let vm = this
      axios({
        url: `${base_url}/proxy/qywx/jsdkConfig`,
        method: 'post',
        data: qs.stringify(
          // { url: 'http://wycl.autocnd.com:41080/ysc/assessManageDetail' },
          { url: location.href.split('?')[0], type: 'app' },
          { encode: false },
        ),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          satoken: getCookies('token'),
        },
      }).then((res) => {
        console.log('jsdkConfig:', res)
        let jsdk = res.data.data
        localStorage.setItem('jsdk', JSON.stringify(res.data.data))
        wx.agentConfig({
          corpid: jsdk.corpid, // 必填，企业微信的corpid，必须与当前登录的企业一致
          agentid: jsdk.agentid, // 必填，企业微信的应用id （e.g. 1000247）
          timestamp: jsdk.timestamp, // 必填，生成签名的时间戳
          nonceStr: jsdk.nonceStr, // 必填，生成签名的随机串
          signature: jsdk.signature, // 必填，签名，见附录-JS-SDK使用权限签名算法
          jsApiList: ['openEnterpriseChat'], //必填，传入需要使用的接口名称
          success: function (res) {
            console.log('agentConfig权限成功返回信息:', res)
            wx.openEnterpriseChat({
              // 注意：userIds和externalUserIds至少选填一个。内部群最多2000人；外部群最多500人；如果有微信联系人，最多40人
              userIds: '', //参与会话的企业成员列表，格式为userid1;userid2;...，用分号隔开。
              externalUserIds: data.externalUserid, // 参与会话的外部联系人列表，格式为userId1;userId2;…，用分号隔开。
              groupName: '', // 会话名称。单聊时该参数传入空字符串""即可。
              chatId: '', // 若要打开已有会话，需指定此参数。如果是新建会话，chatId必须为空串
              success: function (res) {
                var chatId = res.chatId //返回当前群聊ID，仅当使用agentConfig注入该接口权限时才返回chatId
                // 回调
              },
              fail: function (res) {
                if (res.errMsg.indexOf('function not exist') > -1) {
                  alert('版本过低请升级')
                }
              },
            })
          },
          fail: function (res) {
            console.log('agentConfig权限失败返回信息:', res)
            if (res.errMsg.indexOf('function not exist') > -1) {
              alert('版本过低请升级')
            }
          },
        })
      })
    },
    onClickLeft() {
      this.$router.go(-1)
    },
    // 详情
    getCarInfoBase(id) {
      carApi.getCarInfoBase({ id }).then((res) => {
        console.log(res)
        if (res.code == 1) {
          res.data.label = res.data.label ? res.data.label.split(';') : []
          if (res.data && res.data.label && res.data.label.length != 0) {
            res.data.label.length = res.data.label.length - 1
          }
          res.data.memberTies = res.data.memberTies ? '是' : '否'
          res.data.resSell = res.data.resSell ? '是' : '否'
          res.data.rmember = res.data.rmember ? '是' : '否'
          this.dataSource = res.data
        }
      })
    },
    // 卡券
    getCoupon(id) {
      repairApi.getCoupon({ id, isReturnInvalidCoupons: '是' }).then((res) => {
        console.log(res)
        if (res.code == 1) {
          this.cardList = res.data
        }
      })
    },
    // 履历
    getRepairworkorder(id) {
      carApi.getRepairworkorder({ id }).then((res) => {
        console.log(res)
        if (res.code == 0) {
          this.repairList = res.data
        }
      })
    },

    //维修预约
    applyRepair() {
      this.$router.push({
        path: '/applyRepairEdit',
        query: { carId: this.billId },
      })
    },
    // 附件管理
    goFile() {
      this.$router.push({ path: 'fileMange', query: { billId: this.billId } })
    },

    handleTel(tel) {
      if (isPhone()) {
        window.location.href = `tel:${tel}`
      } else {
        Toast('请在手机端使用此功能')
      }
    },
    goRepairDetail(data) {
      this.$router.push({
        path: '/repairOrderDetail',
        query: { billId: data.repairId },
      })
    },
    handleInsurance() {
      if (this.dataSource.inusuranceId) {
        this.$router.push({
          path: '/policyRobotDetail',
          query: { billId: this.dataSource.inusuranceId },
        })
      }
    },
  },
}
</script>

<style lang="less" scoped>
.flex {
  display: flex;
}

.registerDetail {
  background: #f3f3f3;

  .detail_header {
    justify-content: space-between;
    margin: 0.2rem 0.2rem;
    border-radius: 5px;
    background: #fff;
    padding: 0.3rem;

    .detail_header_lt {
      .carNo {
        font-size: 0.32rem;
        font-weight: bold;
        margin-bottom: 0.2rem;

        .icon-right {
          margin-left: 0.2rem;
          color: #409eff;
        }
      }

      .hank {
        font-size: 0.28rem;
        color: #666;
      }
    }

    .detail_header_rt {
      justify-content: flex-start;

      .txt {
        font-size: 0.28rem;
        color: #161616;
      }

      .icon-tel {
        color: #fff;
        background: #35bc04;
        font-size: 0.32rem;
        padding: 0.1rem;
        border-radius: 50%;
        margin-right: 0.1rem;
      }
    }
  }

  .formData {
    background: #fff;
  }
}

/deep/ .label_box .van-field__control--custom {
  display: inline !important;
}

.label_item {
  color: #35bc04;
  border: 1px solid #35bc04;
  border-radius: 4px;
  margin-right: 0.1rem;
  min-width: 0.4rem;
  padding: 0.01rem 0.1rem;
  text-align: center;
  font-size: 0.2rem;
  float: left;
  margin-bottom: 0.1rem;

  &:last-child {
    margin-right: 0;
  }
}

.tabBarBox {
  margin-top: 0.2rem;
}

.tabCar {
  margin-top: 0.1rem;
  padding-bottom: 1.3rem;
}

// 卡券
.cardList {
  margin: 0.1rem 0.2rem 0.3rem 0.2rem;
  box-sizing: border-box;

  .cardItem {
    background: #fff;
    border-radius: 10px;
    box-sizing: border-box;
    overflow: hidden;
    margin-bottom: 0.2rem;

    .box {
      overflow: hidden;

      .cardItem_lt {
        padding: 0.3rem 0.2rem;
        text-align: center;
        box-sizing: content-box;
        background: #ff91d7;
        color: #fff;
        overflow: hidden;
        height: 1.4rem;
        width: 30%;

        .price_icon {
          font-size: 0.3rem;
          color: #fff;
        }

        .price {
          color: #fff;

          .txt {
            font-size: 0.8rem;
            font-weight: bold;
          }

          .name {
            font-size: 0.28rem;
          }
        }
      }

      .cardItem_rt {
        padding: 0.3rem;
        border-bottom: 1px dashed #ccc;
        overflow: hidden;
        height: 1.4rem;
        width: 70%;

        .label_name {
          .label_txt {
            background: #ff3415;
            color: #fff;
            border-radius: 3px;
            text-align: center;
            padding: 0.07rem 0.2rem;
            font-size: 0.24rem;
            margin-right: 0.1rem;
          }

          .label_txt1 {
            color: #333;
            font-size: 0.28rem;
            font-weight: bold;
          }
        }

        .label_time {
          margin-top: 0.2rem;
          font-size: 0.26rem;
          color: #666;
        }
      }
    }

    .cardNo {
      padding: 0.2rem 0.3rem 0.2rem 0.2rem;
      font-size: 0.28rem;
      color: #333;
    }
  }
}

// 维修
.repairData {
  margin: 0.1rem 0.2rem 0.3rem 0.2rem;
  box-sizing: border-box;
  overflow: hidden;

  .repairItem {
    background: #fff;
    overflow: hidden;
    border-radius: 5px;
    padding: 0.2rem;
    margin-bottom: 0.2rem;

    .item_title {
      justify-content: space-between;
      padding: 0.1rem 0.1rem 0.2rem 0.3rem;
      border-bottom: 1px solid #f1f1f1;
      font-size: 0.28rem;
      margin-bottom: 0.3rem;
      position: relative;

      &::before {
        content: '';
        display: block;
        width: 3px;
        height: 15px;
        background: #409eff;
        position: absolute;
        left: 0;
        top: 5px;
      }
    }

    .item_content {
      padding: 0 0.3rem;

      .item_contentbox {
        justify-content: space-between;
      }

      .item_data {
        font-size: 0.28rem;
        margin-bottom: 0.2rem;
        display: flex;

        &.item_remark {
          display: block;
        }

        .item_data_label {
          color: #808080;
        }

        .item_data_value {
          color: #333;

          &.txt {
            line-height: 0.32rem;
          }
        }
      }

      .item_data_width {
        float: left;
      }
    }
  }
}

// 联系人
.contactbox {
  margin: 0.1rem 0.2rem 0.3rem 0.2rem;
  padding-bottom: 0.1rem;

  .contactItem {
    justify-content: space-between;
    background: #fff;
    padding: 0.2rem;
    margin-bottom: 0.1rem;
    border-radius: 4px;

    .contact_lt {
      .header {
        margin-bottom: 0.2rem;
        color: #333;

        .txt {
          font-size: 0.2rem;
          margin-right: 0.2rem;
          background: #409eff;
          padding: 0.1rem 0.2rem;
          border-radius: 4px;
          color: #fff;
        }

        .name {
          font-size: 0.28rem;
        }
      }

      .tel {
        font-size: 0.3rem;
        color: #333;
      }
    }

    .contact_rt {
      .icon-wx {
        font-size: 0.7rem;
        color: #409eff;
      }

      .icon-tel {
        font-size: 0.36rem;
        background: #35bc04;
        color: #fff;
        text-align: center;
        padding: 0.1rem;
        border-radius: 50%;
        position: relative;
        top: -6px;
        margin-right: 0.1rem;
      }
    }
  }
}

.addBtn {
  position: fixed;
  bottom: 0rem;
  width: 100%;
  z-index: 1;
  background: #fff;
  padding: 0.2rem;

  .orderItem_filebox {
    width: 2.8rem;
    margin: auto;
    text-align: center;
    font-size: 0.28rem;
    background: #409eff;
    color: #fff;
    padding: 0.25rem 0rem;
    border-radius: 50px;
  }
}

.noContent {
  margin-top: 0rem;
}

.contact_list {
  padding: 0.5rem 0.2rem 0 0.2rem;

  .contact_item {
    justify-content: space-between;
    margin-bottom: 20px;
  }

  .contact_lt {
    font-size: 0.28rem;
    color: #23ec1c;

    .contact_header {
      width: 30px;
      height: 30px;
      display: block;
      border-radius: 5px;
      margin-right: 5px;
    }

    .contact_name {
      font-size: 0.28rem;
      color: #333;
      margin-left: 5px;
    }

    .contact_com {
      color: #f65e07;
      font-size: 0.28rem;
    }

    .contact_com_wx {
      color: #23ec1c;
      font-size: 0.28rem;
    }
  }

  .contact_btn {
    background: #409eff;
    color: #fff;
    padding: 5px 15px;
    border-radius: 3px;
    font-size: 0.28rem;
  }
}
</style>
